/*****************************************************************
/     filename:   showheaders.l
/
/  description:   Implements a lex filter to display headers
/
/       author:   Szechy, Jared
/     login id:   cps444-n1.10
/
/        class:   CPS 444
/   instructor:   Perugini
/   assignment:   Homework #7
/
/     assigned:   November 18, 2009
/          due:   November 23, 2009
/
/***************************************************************/
extern int yy_flex_debug;

alphanumeric [0-9_A-Za-z]

%{
#include<stdio.h>
#include<unistd.h>
int uflag = 0;
int cflag = 0;
int printc = 0;
int printu = 0;
int c;
%}

%%

["<]{alphanumeric}+[.]h[">]     { if(printu == 1) printf ("%s\n", yytext); }
\/\/[:space:]*(["<]{alphanumeric}+[.]h[">])     { if(printc == 1) printf ("%s\n", yytext); }
\/\*[:space:]*(["<]{alphanumeric}+[.]h[">])     { if(printc == 1) printf ("%s\n", yytext); }
.				{ }
\n				{ }

%%

/* called by lex when EOF reached */
int yywrap (void) {
   /* convention is to return 1 */
   return 1;
}

int main (int argc, char** argv) {
   /* main entry point for lex */
   yy_flex_debug = 0;
   while ((c = getopt(argc, argv, "uc")) != -1)
      switch (c)
      {
         case 'u':
            uflag = 1;
            break;
         case 'c':
            cflag = 1;
            break;
         case '?':
            if (isprint(optopt))
               fprintf(stderr, "showheaders: invalid option -- '%c'\n", optopt);
            else
               fprintf(stderr, "Unknown option character `\\x%x;.\n", optopt);
            return 1;
         default:
            abort();
      }
   
   if ( uflag == 0 && cflag == 0)
   {
      printf("Uncommented header filenames:\n");
      printu = 1;
      yylex();
      printu = 0; printc = 1;
      printf("\nCommented header filenames:\n");
      yylex();
   }
   else if (uflag == 1)
   {
      printf("Uncommented header filenames:\n");
      printu = 1;
      yylex();
   }
   else if (cflag == 1)
   {
      printf("Commented header filenames:\n");
      printc = 1;
      yylex();
   }
   return 0;
}
